//
//  IndexViewController.m
//  atongmuChildren
//
//  Created by 度周末网络-王腾 on 16/6/2.
//  Copyright © 2016年 dzmmac. All rights reserved.
//

#import "IndexViewController.h"
#import "IndexTopCollectionViewCell.h"
#import "IndexMainCollectionViewCell.h"
#import "EmptyView.h"
#import "IndexViewModel.h"
#import "UINavigationBar+Awesome.h"
#import "RDVTabBarController.h"
#import "RDVTabBarItem.h"
#define kIndexHeaderViewHeight kScreenW*0.6
#import "IndexConfig.h"
#import "CycleScrollView.h"
#import "WTScrollView.h"

@interface IndexViewController ()  <EmptyReloadDelegate>


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *banner_bottom_layout;

@property (weak, nonatomic) IBOutlet UICollectionView *mainCollectionView;
@property (nonatomic,strong) EmptyView *emptyView;
@property (nonatomic,strong) IndexViewModel *indexViewModel;

@property (weak, nonatomic) IBOutlet UIView *bannerBackgroundView;
@property (weak, nonatomic) IBOutlet WTScrollView *bannerView;
@property (nonatomic , retain) CycleScrollView *mainScorllView;
@end

@implementation IndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configCollectionLayout];

    
    self.emptyView = [EmptyView getInstance];
    self.emptyView.delegate=self;
    [self setCustomLeftButton:self action:@selector(leftclick) img:@"logo_2x"];
    self.indexViewModel = [[IndexViewModel alloc ] init];
    
    self.mainCollectionView.alpha = 0;
    [self laodData];
}

-(void)leftclick{
    
}

-(void)emptyReloadBtnAction{
    [self laodData];
}



-(void)laodData{
    
    if ([CommoneTools isConnectionAvailable]) {
        [self.emptyView removeFromSuperview];
    } else {
        [self.emptyView showByView:self.view];
        return;
    }
    
    [self.indexViewModel loadIndexData:^(NSInteger resultType, id data, NSString *errorMsg, NSString *code) {
        [self configBannerData];
         [self setUpNotice];
        self.mainCollectionView.alpha = 1;
        [self.mainCollectionView reloadData];
        
       
    }];
}

/**
 *  解析广告数据
 */
-(void)configBannerData{
    
    if (self.indexViewModel.bannerStrings.count > 0) {
        
        [self.bannerView setPageControlFrame:CGRectMake(0,
                                                          self.bannerView.frame.size.height - 20.0f,
                                                          [UIScreen width] ,
                                                          20.0f)
                                  curPageColor:[UIColor whiteColor]
                                   normalColor:[UIColor colorWithWhite:1 alpha:0.5]];
        
        [self.bannerView setScrollImageUrls:self.indexViewModel.bannerStrings
                                     duration:4
                                   clickBlock:^(NSInteger clickIndex){
                                       
                                       
                                   }];
     
    }
}


-(void)setUpNotice{
    
    if (self.indexViewModel.indexModel.infomodel.noticeBoardList.count == 0) {
        self.banner_bottom_layout.constant = 0;
        [CommoneTools changeConstraintView:self.bannerBackgroundView constraintHeight:kIndexHeaderViewHeight];
        return;
    }
    
    [CommoneTools changeConstraintView:self.bannerBackgroundView constraintHeight:kIndexHeaderViewHeight+35];
    WEAKSELF
    if (self.mainScorllView == nil) {
        self.mainScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(37, GTFixHeightFloat(130)+5, [UIScreen width]-47, 28) animationDuration:(self.indexViewModel.noticInfoList.count == 1)?INTMAX_MAX:2];
    }
    
    self.mainScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return weakSelf.indexViewModel.noticInfoList[pageIndex];
    };
    self.mainScorllView.totalPagesCount = ^NSInteger(void){
        return weakSelf.indexViewModel.noticInfoList.count;
    };
    
    [self.bannerBackgroundView addSubview:self.mainScorllView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar lt_setBackgroundColor:[utils getThemeColor]];
    [self.navigationController.navigationBar setShadowImage:nil];
}


-(void)configCollectionLayout{
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.mainCollectionView.collectionViewLayout;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
}

#pragma mark  -------  UICollectionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.indexViewModel.sectionTopList.count;
    }else if (section == 1)
        return self.indexViewModel.sectionMiddleList.count;
    else{
        return self.indexViewModel.sectionBottomList.count;;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(([UIScreen mainScreen].bounds.size.width)/kIndexItemCount, GTFixHeightFloat(kScreenW/kIndexItemCount));
    }else if (indexPath.section == 1)
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, GTFixHeightFloat(93)+20);
    else{
       return CGSizeMake(([UIScreen mainScreen].bounds.size.width)/kIndexItemCount, GTFixHeightFloat(kScreenW/kIndexItemCount));
    }
   
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        IndexMainCollectionViewCell *mainCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IndexMainCollectionViewCell" forIndexPath:indexPath];
        mainCell.indexPath = indexPath;
        [mainCell showByModel:[self.indexViewModel.sectionTopList objectAtIndex:indexPath.row]];
        return mainCell;
    }else if (indexPath.section == 1){
        
        IndexTopCollectionViewCell *topCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IndexTopCollectionViewCell" forIndexPath:indexPath];
        [topCell showByModel:[self.indexViewModel.sectionMiddleList objectAtIndex:indexPath.row]];
        
        return topCell;
    }
    else{
        IndexMainCollectionViewCell *mainCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IndexMainCollectionViewCell" forIndexPath:indexPath];
        mainCell.isShowTopLine = self.indexViewModel.sectionMiddleList.count == 0 ? NO :YES;
        mainCell.indexPath = indexPath;
        [mainCell showByModel:[self.indexViewModel.sectionBottomList objectAtIndex:indexPath.row]];
        return mainCell;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
